package com.ruoyi.project.jiaqin.companyautograph.controller;

import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companyautograph.domain.CompanyAutograph;
import com.ruoyi.project.jiaqin.companyautograph.service.ICompanyAutographService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业个性签名Controller
 *
 * @author admin
 * @date 2021-02-19
 */
@Controller
@RequestMapping("/jiaqin/companyautograph")
public class CompanyAutographController extends BaseController
{
    private String prefix = "jiaqin/companyautograph";

    @Autowired
    private ICompanyAutographService companyAutographService;
    @Autowired
    private ICompanyService iCompanyService;

    @RequiresPermissions("jiaqin:companyautograph:view")
    @GetMapping()
    public String companyautograph()
    {
        return prefix + "/companyautograph";
    }

    /**
     * 查询企业个性签名列表
     */
    @RequiresPermissions("jiaqin:companyautograph:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyAutograph companyAutograph)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null){
            companyAutograph.setCompanyId(company.getId());
            startPage();
            List<CompanyAutograph> list = companyAutographService.selectCompanyAutographList(companyAutograph);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyAutograph> list = companyAutographService.selectCompanyAutographList(companyAutograph);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出企业个性签名列表
     */
    @RequiresPermissions("jiaqin:companyautograph:export")
    @Log(title = "企业个性签名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyAutograph companyAutograph)
    {
        List<CompanyAutograph> list = companyAutographService.selectCompanyAutographList(companyAutograph);
        ExcelUtil<CompanyAutograph> util = new ExcelUtil<CompanyAutograph>(CompanyAutograph.class);
        return util.exportExcel(list, "companyautograph");
    }

    /**
     * 新增企业个性签名
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {

        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }
        return prefix + "/add";
    }

    /**
     * 新增保存企业个性签名
     */
    @RequiresPermissions("jiaqin:companyautograph:add")
    @Log(title = "企业个性签名", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyAutograph companyAutograph)
    {
        //限制一个企业只能有一个个性签名
        CompanyAutograph searchCa = new CompanyAutograph();
        searchCa.setCompanyId(companyAutograph.getCompanyId());
        List<CompanyAutograph> list = companyAutographService.selectCompanyAutographList(searchCa);
        if(list.size()>0){
            return AjaxResult.error("只能有一个个性签名");
        }
        return toAjax(companyAutographService.insertCompanyAutograph(companyAutograph));
    }

    /**
     * 修改企业个性签名
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyAutograph companyAutograph = companyAutographService.selectCompanyAutographById(id);
        mmap.put("companyAutograph", companyAutograph);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业个性签名
     */
    @RequiresPermissions("jiaqin:companyautograph:edit")
    @Log(title = "企业个性签名", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyAutograph companyAutograph)
    {
        return toAjax(companyAutographService.updateCompanyAutograph(companyAutograph));
    }

    /**
     * 删除企业个性签名
     */
    @RequiresPermissions("jiaqin:companyautograph:remove")
    @Log(title = "企业个性签名", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyAutographService.deleteCompanyAutographByIds(ids));
    }
}
